package com.example.socialapp.ui.main.screens

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.Divider
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import kotlin.math.absoluteValue

/**
 * 聊天Tab界面
 *
 * @param onNavigateToChatDetail 导航到聊天详情界面回调
 */
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ChatScreen(
    onNavigateToChatDetail: (String, String) -> Unit = { _, _ -> }
) {
    Scaffold(
        topBar = {
            TopAppBar(
                title = { Text("聊天") },
                actions = {
                    IconButton(onClick = { /* 搜索 */ }) {
                        Icon(
                            imageVector = Icons.Default.Search,
                            contentDescription = "搜索"
                        )
                    }
                },
                colors = TopAppBarDefaults.topAppBarColors(
                    containerColor = MaterialTheme.colorScheme.primaryContainer,
                    titleContentColor = MaterialTheme.colorScheme.onPrimaryContainer
                )
            )
        },
        floatingActionButton = {
            FloatingActionButton(
                onClick = { /* 新建聊天 */ },
                containerColor = MaterialTheme.colorScheme.primaryContainer
            ) {
                Icon(
                    imageVector = Icons.Default.Add,
                    contentDescription = "新建聊天"
                )
            }
        }
    ) { innerPadding ->
        LazyColumn(
            modifier = Modifier
                .fillMaxSize()
                .padding(innerPadding)
        ) {
            items(sampleChats) { chat ->
                ChatItem(
                    chat = chat, 
                    onClick = { onNavigateToChatDetail(chat.id, chat.name) }
                )
                Divider(
                    modifier = Modifier.padding(start = 80.dp)
                )
            }
        }
    }
}

/**
 * 单个聊天项
 *
 * @param chat 聊天数据
 * @param onClick 点击回调
 */
@Composable
private fun ChatItem(
    chat: Chat,
    onClick: () -> Unit
) {
    Surface(
        onClick = onClick,
        color = Color.Transparent
    ) {
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(horizontal = 16.dp, vertical = 12.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            // 头像
            Box(
                modifier = Modifier
                    .size(56.dp)
                    .clip(CircleShape)
                    .background(getColorForChat(chat))
            ) {
                Text(
                    text = chat.name.first().toString(),
                    style = MaterialTheme.typography.headlineSmall,
                    color = Color.White,
                    modifier = Modifier.align(Alignment.Center)
                )
            }
            
            Spacer(modifier = Modifier.width(16.dp))
            
            // 聊天信息
            Column(
                modifier = Modifier.weight(1f)
            ) {
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Text(
                        text = chat.name,
                        style = MaterialTheme.typography.titleMedium,
                        fontWeight = FontWeight.Bold,
                        modifier = Modifier.weight(1f)
                    )
                    
                    Text(
                        text = chat.time,
                        style = MaterialTheme.typography.bodySmall,
                        color = MaterialTheme.colorScheme.outline
                    )
                }
                
                Spacer(modifier = Modifier.height(4.dp))
                
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Text(
                        text = chat.lastMessage,
                        style = MaterialTheme.typography.bodyMedium,
                        maxLines = 1,
                        overflow = TextOverflow.Ellipsis,
                        color = if (chat.unreadCount > 0) 
                            MaterialTheme.colorScheme.onSurface
                        else 
                            MaterialTheme.colorScheme.outline,
                        modifier = Modifier.weight(1f)
                    )
                    
                    if (chat.unreadCount > 0) {
                        Box(
                            modifier = Modifier
                                .size(24.dp)
                                .clip(CircleShape)
                                .background(MaterialTheme.colorScheme.primary),
                            contentAlignment = Alignment.Center
                        ) {
                            Text(
                                text = chat.unreadCount.toString(),
                                style = MaterialTheme.typography.bodySmall,
                                color = MaterialTheme.colorScheme.onPrimary
                            )
                        }
                    }
                }
            }
        }
    }
}

/**
 * 根据聊天名称生成一个固定的颜色
 */
private fun getColorForChat(chat: Chat): Color {
    val colors = listOf(
        Color(0xFF6200EA), // 深紫色
        Color(0xFF2962FF), // 深蓝色
        Color(0xFF00BFA5), // 青色
        Color(0xFFFFAB00), // 琥珀色
        Color(0xFFD50000)  // 红色
    )
    
    return colors[chat.name.hashCode().mod(colors.size).absoluteValue]
}

/**
 * 聊天数据类
 */
data class Chat(
    val id: String,
    val name: String,
    val lastMessage: String,
    val time: String,
    val unreadCount: Int = 0
)

/**
 * 示例聊天列表
 */
private val sampleChats = listOf(
    Chat(
        id = "1",
        name = "张三",
        lastMessage = "明天一起去看电影吧？",
        time = "上午11:30",
        unreadCount = 2
    ),
    Chat(
        id = "2",
        name = "李四",
        lastMessage = "项目进展如何了？",
        time = "昨天",
        unreadCount = 1
    ),
    Chat(
        id = "3",
        name = "王五",
        lastMessage = "好的，我会准时到的",
        time = "昨天",
        unreadCount = 0
    ),
    Chat(
        id = "4",
        name = "开发小组",
        lastMessage = "王五: 我已经完成了UI设计，准备开始编码",
        time = "周二",
        unreadCount = 0
    ),
    Chat(
        id = "5",
        name = "赵六",
        lastMessage = "你发的链接我看了，很有帮助",
        time = "周一",
        unreadCount = 0
    ),
    Chat(
        id = "6",
        name = "钱七",
        lastMessage = "下周末有空吗？想请你帮个忙",
        time = "上周",
        unreadCount = 0
    ),
    Chat(
        id = "7",
        name = "孙八",
        lastMessage = "谢谢你的建议，我会考虑的",
        time = "上周",
        unreadCount = 0
    ),
    Chat(
        id = "8",
        name = "周九",
        lastMessage = "新版本的功能真不错！",
        time = "2023/12/20",
        unreadCount = 0
    ),
    Chat(
        id = "9",
        name = "吴十",
        lastMessage = "好久不见，最近怎么样？",
        time = "2023/12/15",
        unreadCount = 0
    )
) 